package com.dss.auto_deploy.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import javax.annotation.Resource;

import static com.dss.auto_deploy.constants.GitlabConstants.*;

/**
 * @Description: rabbitmq配置
 * @ClassName: RabbitConfig
 * @author: dongshoushan
 * @date: 2020年05月26日 16:10
 */
@Configuration
public class RabbitConfig {

    @Resource
    private ConnectionFactory connectionFactory;

    @Bean
    public Queue gitlabQueueName() {
        return new Queue(GITLAB_QUEUE_NAME);
    }
    @Bean
    public Queue pullQueueName() {
        return new Queue(DEPLOY_PULL_QUEUE);
    }
//    @Bean
//    public Queue deployQueueName() {
//        return new Queue(DEPLOAY_QUEUE_NAME);
//    }


    @Bean
    public FanoutExchange gitlabExchangeName() {
        return new FanoutExchange(GITLAB_EXCHANGE_NAME);
    }
    @Bean
    public FanoutExchange deployExchangeName() {
        return new FanoutExchange(DEPLOY_EXCHANGE);
    }
//    @Bean
//    public FanoutExchange depolayExchangeName() {
//        return new FanoutExchange(DEPLOAY_EXCHANGE_NAME);
//    }

    @Bean
    Binding bindingGitExchangeMessage(Queue gitlabQueueName, FanoutExchange gitlabExchangeName) {
        return BindingBuilder.bind(gitlabQueueName).to(gitlabExchangeName);
    }
    @Bean
    Binding bindingDeployExchangeMessage(Queue pullQueueName, FanoutExchange deployExchangeName) {
        return BindingBuilder.bind(pullQueueName).to(deployExchangeName);
    }
//    @Bean
//    Binding bindingDeploayExchangeMessage(Queue deployQueueName, FanoutExchange depolayExchangeName) {
//        return BindingBuilder.bind(deployQueueName).to(depolayExchangeName);
//    }


    //endregion

    /**
     * 多例的RabbitTemplate
     *
     * @return
     */
    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate scopeRabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }


}